package com.sanjiahantang.youjiarole.module.decorate.views.impl;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.complain.views.impl.ComplainActivity;
import com.sanjiahantang.youjiarole.module.decorate.adapters.FlowAdapter;
import com.sanjiahantang.youjiarole.module.decorate.beans.DecorateManageBean;
import com.sanjiahantang.youjiarole.module.decorate.presenters.DecorateManagePresenter;
import com.sanjiahantang.youjiarole.module.decorate.views.DecorateManageView;
import com.sanjiahantang.youjiarole.module.programmeprice.views.impl.ProgramChoiceInfoActivity;
import com.sanjiahantang.youjiarole.module.programmeprice.views.impl.ProgramOrderActivity;
import com.sanjiahantang.youjiarole.umengshare.UmengShareManager;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.util.HashMap;


/**
 * Created by zy on 2017/8/11.
 */

public class DecorateDetailActivity extends BaseMvpActivity<DecorateManageView, DecorateManagePresenter> implements DecorateManageView, View.OnClickListener {


    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    //装修id
    private DecorateManageBean.Data decorateData;
    private String requireId;
    private SwipeRefreshLayout refreshLayout;
    private View rlButtons, rlMinus, rlShare, rlAdd, rlComplain,rlAction;
    private ImageView ivArcher;

    @Override
    public DecorateManagePresenter initPresenter() {
        return new DecorateManagePresenter();
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        requireId = getIntent().getStringExtra(KEY_ID);
        presenter.searchProjectInfoForRole($this, requireId, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                if (content != null && content instanceof DecorateManageBean.Data) {
                    decorateData = (DecorateManageBean.Data) content;

                    if (decorateData != null) {
                        closeButtonPanel();
                    } else {
                        Utils.showToast($this, "工程获取失败");
                        finish();
                        return;
                    }

                    if (decorateData != null) {

                        boolean flag = false;
                        if (decorateData.getCompletionList() != null && decorateData.getCompletionList().size() > 0) {
                            if (decorateData.getCompletionList().get(decorateData.getCompletionList().size() - 1).getStatusId() >= 156) {
                                flag = true;
                            }
                        }
                        if (flag) {
                            rlComplain.setVisibility(View.GONE);
                            rlMinus.setVisibility(View.GONE);
                            rlAdd.setVisibility(View.GONE);
                        } else if (decorateData.getIsOfficial() == 1) {//官方

                            String phone = SPUtils.get($this, SPConfig.PHONE, "").toString();

                            String role = "";
                            if (decorateData.getIsSubcontractProject() == 1){
                                if (phone.equals(decorateData.getProjectPhone())) {
                                    //项目经理
                                    role += "1,";
                                }
                            }else {
                                if (phone.equals(decorateData.getRoleProjectPhone())) {
                                    //项目经理
                                    role += "1,";
                                }
                            }


                            if (phone.equals(decorateData.getRoleSupervisionPhone())) {
                                //监理
                                role += "2,";
                            }

                            if (phone.equals(decorateData.getRoleDesignerPhone())) {
                                //设计师
                                role += "0,";
                            }

                            rlAction.setVisibility(View.VISIBLE);
                            if (role.contains("1")){
                                rlMinus.setVisibility(View.GONE);
                            }else if(role.contains("2")){
                                rlMinus.setVisibility(View.GONE);
                                rlAdd.setVisibility(View.GONE);
                            }else if(role.contains("0")){
                                rlComplain.setVisibility(View.GONE);
                                rlMinus.setVisibility(View.GONE);
                                rlAdd.setVisibility(View.GONE);
                                rlAction.setVisibility(View.GONE);//分享隐藏后隐藏按钮
                            }else{
                                rlAction.setVisibility(View.GONE);
                            }

                        } else {//非官方
                            rlAction.setVisibility(View.GONE);//分享隐藏后隐藏按钮
                            rlComplain.setVisibility(View.GONE);
                            rlMinus.setVisibility(View.GONE);
                            rlAdd.setVisibility(View.GONE);
                        }
                    }

                    recyclerView.setAdapter(new FlowAdapter($this, decorateData, presenter));
                } else if (content instanceof Integer && (int) content == CALLBACK_TYPE_RECOVER) {
                    onResume();
                } else {
                    Utils.showToast($this, "获取工程失败");
                    finish();
                }

            }
        });
    }

    @Override
    public void initView() {
        setContentView(R.layout.fragment_recycler_list);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        layoutManager = new LinearLayoutManager($this);
        recyclerView.setLayoutManager(layoutManager);

        TextView title = (TextView) findViewById(R.id.title);
        title.setText("装修管理");
        findViewById(R.id.fl_Left).setOnClickListener(this);

        ivArcher = (ImageView) findViewById(R.id.ivArcher);
        rlAction = findViewById(R.id.rlAction);
        rlAction.setOnClickListener(this);
        rlButtons = findViewById(R.id.rlButtons);
        rlComplain = findViewById(R.id.rlComplain);
        rlComplain.setOnClickListener(this);
        rlAdd = findViewById(R.id.rlAdd);
        rlAdd.setOnClickListener(this);
        rlMinus = findViewById(R.id.rlMinus);
        rlMinus.setOnClickListener(this);
        findViewById(R.id.rlShare).setOnClickListener(this);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onResume();
            }
        });
    }


    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public void hideProgress() {
        super.hideProgress();
        refreshLayout.setRefreshing(false);
    }

    @Override
    public DecorateManagePresenter getPresenter() {
        return presenter;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.rlAction:
                if (!"1".equals(ivArcher.getTag(R.id.decode))) {
                    RotateAnimation rotateAnimation = new RotateAnimation(0.0f, +180.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    rotateAnimation.setDuration(500);
                    rotateAnimation.setFillAfter(true);
                    rotateAnimation.setRepeatCount(0);
                    ivArcher.setTag(R.id.decode, "1");
                    ivArcher.startAnimation(rotateAnimation);

                    rlButtons.setVisibility(View.VISIBLE);
                    TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.5f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
                    translateAnimation.setRepeatCount(0);
                    translateAnimation.setDuration(500);

                    rlButtons.startAnimation(translateAnimation);
                } else {
                    closeButtonPanel();
                }

                break;
            case R.id.rlComplain:
                startActivity(new Intent($this, ComplainActivity.class)
                        .putExtra(KEY_PARAM, decorateData)
                );
                closeButtonPanel();
                break;
            case R.id.rlAdd:
                String username = null;
                for (HashMap<String, DecorateManageBean.Role> m : decorateData.getRoleList()) {
                    for (String k : m.keySet()) {
                        if (k.equals("designer")) {
                            username = m.get(k).getUserName();
                        }
                    }
                }
                startActivity(new Intent($this, ProgramChoiceInfoActivity.class).putExtra("PPID", decorateData.getPpId())
                        .putExtra("customerphone", decorateData.getCustomerPhone()).putExtra("designphone", decorateData.getRoleDesignerPhone())
                        .putExtra("designname", username));
                closeButtonPanel();
                break;
            case R.id.rlMinus:
                startActivity(new Intent($this, ProgramOrderActivity.class)
                                .putExtra(KEY_ID, decorateData.getPpId())
                        // .putExtra(KEY_TYPE, TYPE_INCREASE)
                );
                closeButtonPanel();
                break;
            case R.id.rlShare:
                UmengShareManager.share(this, "");
                closeButtonPanel();
                break;
        }
    }

    private void closeButtonPanel() {
        RotateAnimation rotateAnimation = new RotateAnimation(+180.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(500);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setRepeatCount(0);
        ivArcher.setTag(R.id.decode, "2");
        ivArcher.startAnimation(rotateAnimation);


        TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.5f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
        translateAnimation.setRepeatCount(0);
        translateAnimation.setDuration(500);
        translateAnimation.setFillAfter(true);

        rlButtons.startAnimation(translateAnimation);
    }
}
